package net.xdclass.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.xdclass.enums.ShortLinkStateEnum;
import net.xdclass.manager.GroupCodeMappingManager;
import net.xdclass.mapper.GroupCodeMappingMapper;
import net.xdclass.model.GroupCodeMappingDO;
import net.xdclass.vo.GroupCodeMappingVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class GroupCodeMappingManagerImpl implements GroupCodeMappingManager {
	@Autowired
	private GroupCodeMappingMapper groupCodeMappingMapper;

	@Override
	public GroupCodeMappingDO findByGroupIdAndMappingId(Long mappingId, Long accountNo, Long groupId) {
		GroupCodeMappingDO groupCodeMappingDO = groupCodeMappingMapper.selectOne(new QueryWrapper<GroupCodeMappingDO>()
				.eq("account_no",accountNo).eq("id",mappingId)
		        .eq("group_id",groupId));
		return groupCodeMappingDO;
	}

	@Override
	public GroupCodeMappingDO findByCode(String code) {
		GroupCodeMappingDO groupCodeMappingDO = groupCodeMappingMapper.selectOne(new QueryWrapper<GroupCodeMappingDO>()
				.eq("code",code));
		return groupCodeMappingDO;
	}

	@Override
	public int add(GroupCodeMappingDO groupCodeMappingDO) {
		int  rows = groupCodeMappingMapper.insert(groupCodeMappingDO);
		return rows;
	}

	@Override
	public int del(String shortLinkCode, Long accountNo, Long groupId) {
		int rows = groupCodeMappingMapper.update(null,new UpdateWrapper<GroupCodeMappingDO>()
		.eq("code",shortLinkCode)
		.eq("account_no",accountNo)
		.eq("group_id",groupId)
		.set("del","1"));
		return rows;
	}

	@Override
	public Map<String, Object> pageShortLinkByGroupId(Integer page, Integer size, Long accountNo, Long groupId) {
		Page <GroupCodeMappingDO> pageInfo  = new Page(page,size);
		Page <GroupCodeMappingDO> groupPage = groupCodeMappingMapper.selectPage(pageInfo,new QueryWrapper<GroupCodeMappingDO>()
		.eq("account_no",accountNo).eq("group_id",groupId));
		Map <String,Object> pagemap = new HashMap();
		pagemap.put("total_record",groupPage.getTotal());
		pagemap.put("total_page",groupPage.getPages());
		pagemap.put("current_data",groupPage.getRecords().stream().map(obj->beanProcess(obj)).collect(Collectors.toList()));
        return pagemap;
	}






	@Override
	public int updateGroupCodeMappingState(Long accountNo, Long groupId, String shortLinkCode, ShortLinkStateEnum shortLinkStateEnum) {

		int rows = groupCodeMappingMapper.update(null,new UpdateWrapper<GroupCodeMappingDO>()
		.eq("account_no",accountNo)
		.eq("group_id",groupId)
		.eq("code",shortLinkCode)
		.set("state",shortLinkStateEnum.name()));
		return rows;
	}

	@Override
	public int update(GroupCodeMappingDO groupCodeMappingDO) {

		int rows = groupCodeMappingMapper.update(null, new UpdateWrapper<GroupCodeMappingDO>()
				.eq("id", groupCodeMappingDO.getId())
				.eq("account_no", groupCodeMappingDO.getAccountNo())
				.eq("group_id", groupCodeMappingDO.getGroupId())
				.eq("del", 0)
				.set("title", groupCodeMappingDO.getTitle())
				.set("domain", groupCodeMappingDO.getDomain())
		);
		return  rows;
	}

	private static  GroupCodeMappingVO beanProcess(GroupCodeMappingDO groupCodeMappingDO){
		GroupCodeMappingVO groupCodeMappingVO = new GroupCodeMappingVO();
		BeanUtils.copyProperties(groupCodeMappingDO,groupCodeMappingVO);
		return groupCodeMappingVO;
	}
}
